//file:noinspection ALL
pipeline {
    agent any

    environment {
        projectName = 'clever-ext-classic'
        // 源码配置
        git = 'https://github.com/Lzw2016/clever-ext-classic.git'
        gitCredentials = 'github_lizw'
        // 构建配置
        baseDir = "${WORKSPACE}"
        profiles = 'dev'
        port = "31000"
        // 自定义环境变量
        JAVA_HOME = '/opt/jenkins-agent/env/java/jdk-17.0.9'
        GRADLE_HOME = '/opt/jenkins-agent/env/gradle/gradle-6.8.3'
        MAVEN_HOME = '/opt/jenkins-agent/env/maven/apache-maven-3.9.6'
        NODE_HOME = '/opt/jenkins-agent/env/nodejs/node-v20.10.0-linux-x64'
        PNPM_HOME="$HOME/.local/share/pnpm"
        PATH = "$JAVA_HOME/bin:$GRADLE_HOME/bin:$MAVEN_HOME/bin:$NODE_HOME/bin:$PNPM_HOME:$HOME/.nvm/versions/node/v20.19.3/bin:$PATH"
    }

    stages {
        stage('#检查环境') {
            steps {
                sh """
                    node -v
                    yarn -v
                """
            }
        }

        stage('#停止服务') {
            steps {
                sh """
                    pid=`ps -ef | grep "${baseDir}/server/server.js" | grep -v 'grep' | awk '{print \$2}'`
                    if [ -z "\$pid" ];then
                        echo '${projectName}未运行'
                    else
                        ps -ef | grep "${baseDir}/server/server.js" | grep -v 'grep' | awk '{print \$2}' | xargs kill
                        echo '${projectName}已停止!'
                        sleep 3s
                        pid=`ps -ef | grep "${baseDir}/server/server.js" | grep -v 'grep' | awk '{print \$2}'`
                        if [ -n "\$pid" ];then
                            ps -ef | grep "${baseDir}/server/server.js" | grep -v 'grep' | awk '{print \$2}' | xargs kill -9
                            echo '${projectName}已停止(强制)!'
                            sleep 1s
                        fi
                    fi
                """
            }
        }

        stage('#拉取代码') {
            steps {
                checkout([
                        $class           : 'GitSCM',
                        branches         : [[name: '*/main']],
                        userRemoteConfigs: [[url: "${git}", credentialsId: "${gitCredentials}"]],
                        extensions       : [],
                ])
            }
        }

        stage('#编译项目') {
            steps {
                sh """
                    npm config set registry https://registry.npmjs.org/
                    yarn config set registry https://registry.npmjs.org/ -g
                    cd $baseDir
                    yarn install
                    yarn run build-preview
                """
            }
        }

        stage('#部署服务') {
            steps {
                withEnv(['JENKINS_NODE_COOKIE=dontkillme']) {
                    sh """
                        nohup node $baseDir/server/server.js -P ${port} -T http://127.0.0.1:31001 -F /data/logs/clever-ext-classic/cacheData.json >> /dev/null 2>&1 &
                    """
                }
            }
        }
    }
}
